﻿namespace NetFx.MSIL
{
  using System;
  using System.Text;
  using System.Threading;
  using System.Collections;

  using System.Reflection;
  using System.Reflection.Emit;

  public class ParameterGenerator:AnyGenerator
  {
    #region Statics
    public static ParameterGenerator of(ParameterBuilder builder, Type parameterType)
    {
      return new ParameterGenerator(builder,parameterType);
    }
    #endregion
    #region Fields
    protected ParameterBuilder builder = null;
    protected object defaultValue = null;
    protected Type parameterType = null;
    #endregion
    #region Properties
    public override object AnyBuilder
    {
      get { return this.builder; }
    }
    public override DefType GeneratorType
    {
      get
      {
        return DefType.Parameter;
      }
    }
    public virtual ParameterBuilder Builder
    {
      get
      {
        return this.builder;
      }
    }
    public virtual string Name
    {
      get
      {
        return this.Builder.Name;
      }
    }
    public virtual Type ParameterType
    {
      get
      {
        return this.parameterType;
      }
    }
    public virtual int Index
    {
      get
      {
        return this.Builder.Position;
      }
    }
    public virtual ParameterToken ParameterToken
    {
      get
      {
        return this.Builder.GetToken();
      }
    }
    public virtual ParameterAttributes Attributes
    {
      get
      {
        return (ParameterAttributes) this.Builder.Attributes;
      }
    }
    public virtual bool IsIn
    {
      get
      {
        return this.Builder.IsIn;
      }
    }
    public virtual bool IsOut
    {
      get
      {
        return this.Builder.IsOut;
      }
    }
    public virtual bool IsOptional
    {
      get
      {
        return this.Builder.IsOptional;
      }
    }
    public virtual object DefaultValue
    {
      get
      {
        return this.defaultValue;
      }
      set
      {
        this.Builder.SetConstant(this.defaultValue = value);
      }
    }
    #endregion
    #region Constructors
    public ParameterGenerator(ParameterBuilder builder,Type parameterType)
    {
      if (builder == null) throw new ArgumentNullException("builder can not be null!");
      if (parameterType == null) throw new ArgumentNullException("parameterType can not be null!");
      this.builder = builder;
      this.parameterType = parameterType;
    }
    #endregion
    #region Utils
    protected override void Dispose(bool disposing)
    {
      if (disposing)
      {
        this.defaultValue = null;
        this.builder = null;
        this.parameterType = null;
      }
      base.Dispose(disposing);
    }
    #endregion
  }
}
